// pages/oneself/oneself.js
const app = getApp()
const connector = require('../../utils/connector.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    nickName: "点击头像登录",
    authorization:'',
    avatarUrl:false,
    identity: false,//身份：false：学员（默认）true：教练
    vip_status:0,
  },
  //测试：身份转换
  identity(e){
    this.setData({
      identity: !app.globalData.identity
    })
    app.globalData.identity = !app.globalData.identity

  },
  //消息订阅
  subscription(e){
    let tmplId1 = 'heMoYXFW8SqixBECXeRUnSAhWY0hec8xwPdoix3pk_M'//注册审核结果通知
    let tmplId3 = 'r5sKumwIVX5XWCNUp0KP1vQIqciZbbPZ11RJ8-fS2cE'//提现结果通知
    let tmplId2 = 'VPmChZMp46UoBgsaffCc8Y-wpU9xjZSicTmpVBwb3M8'//提现审核通知
    let tmplId4 = 'WxNS2-9xzUQcG7iN3bmZ7MD6I3U2PD1a9ctn4SM5kr8'//开通结果通知
    let tmplId5 = 'L5DgcNOyYQ04NFZyJDTNMA3Y_YHlIdktGTUvqPN0Wp4'//订单支付成功通知
    wx.requestSubscribeMessage({
      tmplIds: [tmplId1,tmplId4,tmplId5],
      success (res) { 
        if(res.errMsg == "requestSubscribeMessage:ok"){
          showToasts("操作成功")
        }else{
          showToasts("操作失败，请重试")
        }
      },
      fail:function(res){
        console.log(res)
      }
    })
  },
  //提现通知
  subscription2(e){
    let tmplId1 = 'heMoYXFW8SqixBECXeRUnSAhWY0hec8xwPdoix3pk_M'//注册审核结果通知
    let tmplId3 = 'r5sKumwIVX5XWCNUp0KP1vQIqciZbbPZ11RJ8-fS2cE'//提现结果通知
    let tmplId2 = 'VPmChZMp46UoBgsaffCc8Y-wpU9xjZSicTmpVBwb3M8'//提现审核通知
    let tmplId4 = 'WxNS2-9xzUQcG7iN3bmZ7MD6I3U2PD1a9ctn4SM5kr8'//开通结果通知
    let tmplId5 = 'L5DgcNOyYQ04NFZyJDTNMA3Y_YHlIdktGTUvqPN0Wp4'//订单支付成功通知
    wx.requestSubscribeMessage({
      tmplIds: [tmplId2,tmplId3],
      success (res) { 
        if(res.errMsg == "requestSubscribeMessage:ok"){
          showToasts("操作成功")
        }else{
          showToasts("操作失败，请重试")
        }
        console.log(res)
      },
      fail:function(res){
        console.log(res)
      }
    })
  },
  // //我的信息
  // myMessage(e) {
  //   wx.navigateTo({
  //     url: '/pages/oneself/profile/profile',
  //   })
  // },
  // //我的订单
  // order(e){
  //   wx.navigateTo({
  //     url: './order/order',
  //   })
  // },
  
  //跳页
  formfeed(e){
    // console.log(e.currentTarget.dataset.src)
    wx.navigateTo({
      url: e.currentTarget.dataset.src,
    })
  },

  
  //授权
  getUserInfo(e) {
    var that = this
    var app = getApp()
    // var val = app.searchWord
    if (e.detail.errMsg == "getUserInfo:ok") {
      console.log(e)
      console.log(JSON.stringify(e))
      userWechat(that,e)
      wx.getUserInfo({
        success: res => {
          console.log(res)
          // 可以将 res 发送给后台解码出 unionId
          app.globalData.userInfo = res.userInfo
          that.setData({
            avatarUrl: res.userInfo.avatarUrl,
            nickName: res.userInfo.nickName
          })
          wx.setStorageSync('myself', JSON.parse(res.rawData))
        }
      })
      wx.showToast({
        title: '授权成功...',
        icon: 'none',
        duration: 1000//持续的时间
      })
      // that.setData({
      //   authorizationGet: "false"
      // })
    } else {
      wx.showToast({
        title: '授权失败...',
        icon: 'none',
        duration: 1000//持续的时间
      })
    }

    // util.userinfoget(e, that, "false", '', 'first')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    // queryWebUser(that)
    // console.log(wx.getStorageSync("myself"))
    // if (wx.getStorageSync("myself")) {
    //   that.setData({
    //     avatarUrl: wx.getStorageSync("myself").avatarUrl,
    //     nickName: wx.getStorageSync("myself").nickName
    //   })
    // }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    if(app.globalData.userInfo){
          that.setData({
            avatarUrl: app.globalData.userInfo.avatarUrl,
            nickName: app.globalData.userInfo.nickName
          })
    }
    // let setTime = setInterval(function(){
    //   console.log(app.globalData.identity)
      
        // console.log(userIfo)
        // clearInterval(setTime)
    // },50)
    // userIfo(this)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    let userIfo = wx.getStorageSync("userInfo")

    if(userIfo.coach){
      // console.log(userIfo)
      // console.log(userIfo.vip_status)
      that.setData({
        avatarUrl:userIfo.coach.picture,
        nickName:userIfo.coach.name,
      })
    }
    that.setData({
      identity:app.globalData.identity,
      vip_status:userIfo.vip_status
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },
  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {
    var that = this
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新

  },
})
//微信授权
function userWechat(that,e){
  let obj={
    info:e
  }
    connector.getFunc(connector.connector().userWechat, obj, "post", app.tokens, function (res) {
      console.log(res)
      // if (res.statusCode == 200) {
      //   console.log(res.data)
      // }
    }, function (res) {
      wx.showToast({
        title: "内部错误",
        icon: 'none',
        duration: 1000
      })
    })
}
//获取用户信息
function userIfo(that){
    connector.getFunc(connector.connector().userIfo, "", "get", app.tokens, function (res) {
      console.log(res)
      if (res.statusCode == 200) {
        let identity = false
        if(res.data.user_info.coach){
          identity = true
          that.setData({
            avatarUrl:res.data.user_info.coach.picture,
            nickName:res.data.user_info.coach.nickName,
          })
          console.log(res.data.user_info.coach.picture)
        }else{
          identity = false
          
        }
        app.globalData.identity = identity
          that.setData({
            identity:identity
          })
        console.log(res.data)
      }
    }, function (res) {
      wx.showToast({
        title: "内部错误",
        icon: 'none',
        duration: 1000
      })
    })
}

//提示
function showToasts(str){
  wx.showToast({
    title: str,
    icon: 'none',
    duration: 1000//持续的时间
  })
}